package com.hy.frame.util;

import android.content.Context;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import java.lang.ref.WeakReference;

/**
 * title 定时器
 * author HeYan
 * time 19-7-12 下午4:38
 * desc 主线程
 */
public final class TimerUtil implements MyHandler.HandlerListener {

    private final WeakReference<Context> wCxt;
    private MyHandler handler = null;
    private Runnable callback = null;
    private long milliseconds;
    private LifecycleEventObserver observer;
    private final static int WHAT_TIMER = 101;

    public TimerUtil(Context cxt) {
        if (cxt == null) {
            this.wCxt = null;
            return;
        } else {
            this.wCxt = new WeakReference<>(cxt);
        }
        this.handler = new MyHandler(cxt, this);
        if (cxt instanceof LifecycleOwner) {
            attach((LifecycleOwner) cxt);
        }
    }

    @Override
    public void handleMessage(Message msg) {
        if (msg.what != WHAT_TIMER || this.wCxt == null || this.wCxt.get() == null) return;
        Context cxt = this.wCxt.get();
        if (!PmUtil.isContextExisted(cxt)) return;
        if (this.callback == null) return;
        this.callback.run();
        if (handler != null && milliseconds > 0)
            handler.sendEmptyMessageDelayed(WHAT_TIMER, milliseconds);
    }

    /**
     * 取消订阅
     */
    public void cancel() {
        this.callback = null;
        this.milliseconds = 0;
        if (handler != null)
            handler.removeMessages(WHAT_TIMER);
    }

    /**
     * milliseconds毫秒后执行next操作
     *
     * @param milliseconds 毫秒
     * @param callback     回调
     */
    public void delayed(long milliseconds, Runnable callback) {
        this.callback = callback;
        if (callback == null) return;
        if (handler == null) return;
        this.milliseconds = 0;
        handler.sendEmptyMessageDelayed(WHAT_TIMER, milliseconds);

    }


    /**
     * 每隔milliseconds毫秒后执行
     *
     * @param milliseconds 间隔时间
     * @param callback     回调
     */
    public void interval(long milliseconds, Runnable callback) {
        this.callback = callback;
        if (callback == null) return;
        if (handler == null) return;
        this.milliseconds = milliseconds;
        handler.sendEmptyMessageDelayed(WHAT_TIMER, milliseconds);
    }

    /**
     * 生命周期绑定
     *
     * @param owner LifecycleOwner
     */
    public void attach(LifecycleOwner owner) {
        if (owner != null) {
            if (observer == null) {
                observer = new LifecycleEventObserver() {
                    @Override
                    public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                        if (event == Lifecycle.Event.ON_DESTROY) {
                            source.getLifecycle().removeObserver(observer);
                            observer = null;
                            cancel();
                        }
                    }
                };
            }
            owner.getLifecycle().addObserver(observer);
        }
    }

}
